package org.sang.controller;

import com.github.pagehelper.Page;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.sang.bean.StoreInfo;
import org.sang.dto.StoreInfoTypeRepDto;
import org.sang.service.IStoreInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 门店信息.
 * 〈功能详细描述〉
 *
 * @author Naughty
 * @version 2018/4/4
 * @see StoreInfoController
 * @since 1.0
 */

@RestController
@RequestMapping("/store")
public class StoreInfoController {
    @Autowired
    private IStoreInfoService storeInfoServiceImpl;

    @PostMapping(value = "/add", produces = {MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value="新增门店信息",notes = "新增门店信息")
    public ResponseEntity<StoreInfo> addStoreInfo(@RequestBody StoreInfo storeInfo) {
        return ResponseEntity.ok(storeInfoServiceImpl.insertStoreInfo(storeInfo));
    }

    @DeleteMapping(value = "/delete/{storeId}")
    @ApiOperation(value="删除门店信息",notes = "删除门店信息")
    public ResponseEntity deleteStoreInfo(
        @ApiParam(value = "门店编号",required = true,name = "storeId")
        @PathVariable("storeId") String storeId) {
        storeInfoServiceImpl.deleteStoreInfo(storeId);
        return ResponseEntity.ok().build();
    }


    @PostMapping(value = "/update", produces = {MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value="新增门店信息",notes = "新增门店信息")
    public ResponseEntity<StoreInfo> updateStoreInfo(@RequestBody StoreInfo storeInfo) {
        return ResponseEntity.ok(storeInfoServiceImpl.updateStoreInfo(storeInfo));
    }

    @GetMapping(value = "/query/one/{storeId}")
    @ApiOperation(value="查询门店信息",notes = "查询门店信息")
    public ResponseEntity<StoreInfo> queryOrderConsume(
        @ApiParam(value = "门店编号",required = true,name = "storeId")
        @PathVariable("storeId") String storeId) {
        return ResponseEntity.ok(storeInfoServiceImpl.queryStoreInfo(storeId));
    }

    @PostMapping(value = "/query/list")
    @ApiOperation(value="查询门店信息列表,翻页",notes = "查询门店信息列表,翻页")
    public ResponseEntity<Map<String,Object>> findStoreInfoListByPage(
        @RequestBody StoreInfo storeInfo,
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize){
        Map<String,Object> resultMap= Maps.newConcurrentMap();
        Page<StoreInfo> page = storeInfoServiceImpl.findStoreInfoListByPage(pageNum,pageSize,storeInfo);
        resultMap.put("result", page.getResult());
        resultMap.put("total", page.getTotal());
        return ResponseEntity.ok(resultMap);
    }

    @GetMapping("/query/all/list")
    @ApiOperation(value="查询门店信息列表",notes = "查询门店信息列表")
    public ResponseEntity<List<StoreInfoTypeRepDto>> findStoreInfoAllList(){
            return ResponseEntity.ok(storeInfoServiceImpl.findStoreInfoAllList());
    }
}
